package com.haibei.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.common.uitl.Tool;
import com.haibei.App;
import com.haibei.R;
import com.haibei.activity.SearchActivity;
import com.haibei.adapter.bean.ArticleCategory;
import com.haibei.adapter.bean.MenuItem;
import com.haibei.view.SubTabViewArticle;

public class MenuFragment extends Fragment implements View.OnClickListener {

	private ArrayList<View> mArrayListMenuView;
	private MenuItemClickListener menuItemClickListener;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		menuItemClickListener = new MenuItemClickListener();
		ArrayList<ArticleCategory> list = App.getApp().getCategoryItem();
		View slideView = View.inflate(getActivity(), R.layout.menu_slide_left, null);
		LinearLayout viewContainer = (LinearLayout) slideView.findViewById(R.id.linearLayoutMenus);
		mArrayListMenuView = new ArrayList<View>();
		// ArrayList<MenuItem> ditems = ListUtiles.isEmpty(list) ?
		// createDefaultItem() : createNetItem();//使用网络提供的类别
		ArrayList<MenuItem> ditems = createDefaultItem();
		for (MenuItem mi : ditems) {
			View v = mi.getView();
			v.setOnClickListener(menuItemClickListener);
			mArrayListMenuView.add(v);
			viewContainer.addView(v);
		}
		viewContainer.addView(View.inflate(getActivity(), R.layout.item_splite_h, null));
		slideView.findViewById(R.id.linearLayoutSearch).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Tool.startActivity(getActivity(), SearchActivity.class);
			}
		});
		return slideView;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	private ArrayList<MenuItem> createNetItem() {
		ArrayList<MenuItem> milist = new ArrayList<MenuItem>();
		ArrayList<ArticleCategory> list = App.getApp().getCategoryItem();
		for (ArticleCategory ac : list) {
			milist.add(new MenuItem(getActivity(), ac));
		}
		return milist;
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.relativeLayoutHeadertopic:
			break;
		default:
			break;
		}
	}

	class MenuItemClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			for (View temV : mArrayListMenuView) {
				MenuItem mi = (MenuItem) temV.getTag();
				if (v.equals(temV)) {
					mi.check(mi.getmArticleCategory());
//					SubTabViewConsult.categoryId=mi.getmArticleCategory().getId();//暂时没有别的类别数据
				} else {
					mi.unCheck();
				}
			}
		}
	}

	private ArrayList<MenuItem> createDefaultItem() {
		// 使用默认的菜单项，头条，育儿，医疗健康，食物营养，生活起居
		// 头条 id :1，怀孕分娩id :2，疾病医疗id:3，家庭生活id:4，教育观点id:5，婴幼护理id:6，营养健康id:7
		ArrayList<MenuItem> mlist = new ArrayList<MenuItem>();
		// 头条
		ArticleCategory topic = new ArticleCategory(true);
		topic.setName("头条");
		topic.setIntIconDefault(R.drawable.icon_header);
		topic.setIntIconSelected(R.drawable.icon_header_p);
		topic.setSequence(1);
		topic.setId("1");

		mlist.add(new MenuItem(getActivity(), topic));

		// 育儿
		ArticleCategory feedBabay = new ArticleCategory(true);
		feedBabay.setName("怀孕分娩");
		feedBabay.setIntIconDefault(R.drawable.icon_feed_baby);
		feedBabay.setIntIconSelected(R.drawable.icon_feed_baby_p);
		feedBabay.setSequence(2);
		feedBabay.setId("2");
		mlist.add(new MenuItem(getActivity(), feedBabay));
		// 医疗健康
		ArticleCategory health = new ArticleCategory(true);
		health.setName("疾病医疗");
		health.setIntIconDefault(R.drawable.icon_health);
		health.setIntIconSelected(R.drawable.icon_health_p);
		health.setSequence(3);
		health.setId("3");
		mlist.add(new MenuItem(getActivity(), health));
		// 食物营养
		ArticleCategory food = new ArticleCategory(true);
		food.setName("家庭生活");
		food.setIntIconDefault(R.drawable.icon_food);
		food.setIntIconSelected(R.drawable.icon_food_p);
		food.setSequence(4);
		food.setId("4");
		mlist.add(new MenuItem(getActivity(), food));
		// 生活起居
		ArticleCategory living = new ArticleCategory(true);
		living.setName("教育观点");
		living.setIntIconDefault(R.drawable.icon_life);
		living.setIntIconSelected(R.drawable.icon_life_p);
		living.setSequence(5);
		living.setId("5");
		mlist.add(new MenuItem(getActivity(), living));

		return mlist;
	}
}
